package com.example.wishstar.repository;

import android.os.AsyncTask;
import android.util.Log;

import androidx.lifecycle.LiveData;

import com.example.wishstar.api.Api;
import com.example.wishstar.db.UserDao;
import com.example.wishstar.model.Star;
import com.example.wishstar.model.StarFlag;
import com.example.wishstar.utils.ApiResponse;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class StarRepository {
    private String TAG = "StarRepository";
    private UserDao userDao;
    private Api api;

    public interface StarCallBack {
        void callback1(int postion, Star star);
    }

    public StarRepository(UserDao userDao, Api api) {
        this.userDao = userDao;
        this.api = api;
    }

    public void getAStar(int postion, StarCallBack starCallBack) {
        refreshStar();
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                List<StarFlag> starFlags = userDao.getStarFlagByflag(0);
                if (starFlags.size() > 0) {
                    AsyncTask.execute(new Runnable() {
                        @Override
                        public void run() {
                            Star star = userDao.getStarByWishId(starFlags.get(0).wishId);
                            starCallBack.callback1(postion, star);
                            AsyncTask.execute(new Runnable() {
                                @Override
                                public void run() {
                                    userDao.updateStarFlag(1, starFlags.get(0).starflagId);
                                }
                            });
                        }
                    });
                }
                else {
                    Star star = new Star();
                    starCallBack.callback1(postion, star);
                }

            }
        });
    }


    public void refreshStar() {
        api.getStars().enqueue(new Callback<ApiResponse<List<Star>>>() {
            @Override
            public void onResponse(Call<ApiResponse<List<Star>>> call, Response<ApiResponse<List<Star>>> response) {
                Log.e(TAG, "1");
                if (response.body() != null) {
                    insertStars(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<List<Star>>> call, Throwable t) {
                Log.e(TAG, "2");
            }
        });
    }

    public void insertStars(List<Star> stars) {
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < stars.size(); ++i) {
                    Star star = stars.get(i);
                    Log.e(TAG, star.toString());
                    userDao.insertStar(star);
                    StarFlag starFlag = new StarFlag(star.userId, star.wishId, 0);
                    insertStarFlag(starFlag);
                }
            }
        });
    }

    private void insertStarFlag(StarFlag starFlag) {
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                List<StarFlag> starFlags = userDao.getStarFlagByUserIdAndWishId(starFlag.userId, starFlag.wishId);
                Log.e(TAG, starFlags.toString());
                AsyncTask.execute(new Runnable() {
                    @Override
                    public void run() {
                        if (starFlags.size() == 0) {
                            userDao.insertStarFlag(starFlag);
                        }
                    }
                });
            }
        });
    }

    public void addWishFavourNum(int wishId) {
        api.likeOneWish(wishId).enqueue(new Callback<ApiResponse<Integer>>() {
            @Override
            public void onResponse(Call<ApiResponse<Integer>> call, Response<ApiResponse<Integer>> response) {
                Log.e(TAG, "点赞成功");
                if (response.body() != null) {

                }
            }

            @Override
            public void onFailure(Call<ApiResponse<Integer>> call, Throwable t) {
                Log.e(TAG, "点赞失败");
            }
        });
    }
}
